//
//  AuthorityDeviceCell.m
//  YSSmartHome
//
//  Created by yanghao on 25/05/2017.
//  Copyright © 2017 yahaong. All rights reserved.
//

#import "AuthorityDeviceCell.h"
#import "YSCommon.h"


@interface AuthorityDeviceCell ()
@property (nullable, nonatomic, strong) UIImageView* iconView;
@property (nullable, nonatomic, strong) UILabel* nameLabel;
@property (nullable, nonatomic, strong) UIImageView* selectedIcon;
@end
@implementation AuthorityDeviceCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews
{
    
    self.contentView.layer.borderWidth = 1;
    self.contentView.layer.borderColor = RGBColor(213, 213, 213).CGColor;
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.iconView];
    [self.contentView addSubview:self.selectedIcon];
    [self.contentView addSubview:self.nameLabel];
    [self layoutPageSubviews];
}

- (void)layoutPageSubviews
{
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.size.equalTo(CGSizeMake(kIphoneSize_Widith(50), kIphoneSize_Height(42)));
        make.centerY.equalTo(self.contentView).offset(kIphoneSize_Height(-10));
    }];
    [self.selectedIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(kIphoneSize_Widith(20));
        make.height.equalTo(self.selectedIcon.width);
        make.trailing.equalTo(self.contentView).offset(-5);
        make.top.equalTo(self.contentView).offset(5);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.iconView.bottom).offset(kIphoneSize_Height(10));
    }];
}


#pragma mark - private method 
- (void)setDevice:(ZTDevice *)device
{
    _device = device;
    
    self.nameLabel.text = [device getDeviceName];
    self.selectedIcon.hidden = !device.isAuthority;
    //单路开关
    if (device.devType == ZTDeviceTypeOneWay0Switch
        || device.devType == ZTDeviceTypeOneWaySwitch
        || device.devType == ZTDeviceTypeTwoWay0Switch
        || device.devType == ZTDeviceTypeTwoWaySwitch
        || device.devType == ZTDeviceTypeThreeWay0Switch
        || device.devType == ZTDeviceTypeThreeWaySwitch
        || device.devType == ZTDeviceTypeFourWay0Switch
        || device.devType == ZTDeviceTypeFourWaySwitch)
    {
        if (device.isAuthority )
        {
            self.iconView.image = [UIImage imageNamed:@"home_light_open"];
           
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
        }
    }
    else if (device.devType == ZTDeviceTypeOneWaySocket)
    {
        if (device.isAuthority)
        {
            NSArray<NSString *>  * arrayName = [device.name componentsSeparatedByString:@"."];
            
            if (arrayName.count != 3) {
                self.iconView.image = [UIImage imageNamed:@"home_socket_open_icon0"];
                return;
            }
            NSString *strIcon  = [NSString stringWithFormat:@"home_socket_open_icon%@",arrayName[2]];
            
            self.iconView.image = [UIImage imageNamed:strIcon];
        }
        else
        {
            NSArray * arrayName = [device.name componentsSeparatedByString:@"."];
            if (arrayName.count != 3)
            {
                self.iconView.image = [UIImage imageNamed:@"home_socket_offline_icon0"];
                return;
            }
            NSString *strIcon = [NSString stringWithFormat:@"home_socket_offline_icon%@",arrayName[2]];
            self.iconView.image = [UIImage imageNamed:strIcon];
        }
    }
    //门磁
    else if (device.devType == ZTDeviceTypeZTDoorsensor )
    {
        
        if (device.isAuthority )
        {
            self.iconView.image = [UIImage imageNamed:@"home_doorsensor_open"];
            
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_doorsensor_offline"];
        }
    }
    //乐橙摄像头
    else if (device.devType == ZTDeviceTypeLCCamera )
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_camera_online"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_camera_offline"];
        }
    }
    //红外探测器
    else if (device.devType == ZTDeviceTypeInfraredDetector )
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_infraredDetector_open"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_infraredDetector_offline"];
        }
    }
    //GAS
    else if (device.devType == ZTDeviceTypeGAS)
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_gas_open"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_gas_offline"];
        }
    }
    //一键报警
    else if (device.devType == ZTDeviceTypeAlarmkey)
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_alarm_open"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_alarm_offline"];
        }
    }
    //烟雾探测器
    else if (device.devType == ZTDeviceTypeSmoke)
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_smoke_open"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_smoke_offline"];
        }
    }
    //空气
    else if (device.devType == ZTDeviceTypeAir)
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_air_open"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_air_offline"];
        }
    }
    else if (device.devType == ZTDeviceTypeFlooding)
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_flooding_open"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_flooding_offline"];
        }
    }
    //单轨道窗帘
    else if (device.devType == ZTDeviceTypeCurtainSingleTrack)
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_curtain_open"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_curtain_normal_offline"];
        }
    }
    //双轨道窗帘
    else if (device.devType == ZTDeviceTypeCurtainDoubleTrack)
    {
        if (device.isAuthority)
        {
            self.iconView.image = [UIImage imageNamed:@"home_curtain_open"];
        }
        else
        {
            self.iconView.image = [UIImage imageNamed:@"home_curtain_open_offline"];
        }
    }
    
}

- (UIImageView *)iconView
{
    if (!_iconView) {
        _iconView = [[UIImageView alloc]init];
        _iconView.image = [UIImage imageNamed:@"home_alarm_open"];
    }
    return _iconView;
}

- (UIImageView *)selectedIcon
{
    if (!_selectedIcon) {
        _selectedIcon = [[UIImageView alloc]init];
        _selectedIcon.image = [UIImage imageNamed:@"scene_condition_selected"];
    }
    return _selectedIcon;
}


- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.text = @"测试数据";
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.font = kIOS_Iphone456([UIFont systemFontOfSize:14], [UIFont systemFontOfSize:13], [UIFont systemFontOfSize:12], [UIFont systemFontOfSize:12]);
        _nameLabel.textColor = RGBColor(51, 51, 51);
    }
    return _nameLabel;
}

@end
